package com.twu.biblioteca.menu;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/22/13
 * Time: 8:45 AM
 * To change this template use File | Settings | File Templates.
 */
public class Menu {
    private List<MenuItem> items = new ArrayList<MenuItem>();

    public void show(PrintWriter printWriter){
        for(int i = 0; i < items.size(); i++){
            printWriter.println("\t" + (i+1) + ".  " + items.get(i).getName());
        }
        printWriter.flush();
    }

    public void addMenuItem(MenuItem item) {
        items.add(item);
    }

    public String executeMenuItem(String option) {
        if(option.isEmpty())
            return "";

        int id;
        try {
            id = Integer.valueOf(option);
        }catch (NumberFormatException e){
            return "Select a valid option!!";
        }
        if(id < 1 || id > items.size())
            return "Select a valid option!!";

        MenuItem item = items.get(id - 1);
        if(item == null)
            return "Select a valid option!!";

        String result = item.proceed();
        if(result!=null)
            return result;

        return null;
    }
}
